﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
 
using System.Text;
using System.Windows.Forms;

namespace Demo
{
    public partial class Formula : DemoBaseUserControl
    {
        public Formula()
        {
            InitializeComponent();
            this.Title = "公式";
            this.Description = "Spread .NET 包含了超过300个内置函数，支持跨工作表公式引用，" +
                                 "并提供自定义公式功能。本例中“灰色背景区域”使用了关于债券的相关公式“RECEIVED”和“TBILLPRICE”公式。";
        }

        private void Formula_Load(object sender, EventArgs e)
        {
            this.fpSpread1.DataSource = sheet1TableAdapter.GetData();
            this.formulaTextBox1.Attach(this.fpSpread1);
            this.SetFormla();
            this.InitSpread();

        }

        private void InitSpread()
        {
            this.fpSpread1.Sheets[0].ColumnHeader.Rows[0].Font = new Font("微软雅黑", 9, FontStyle.Bold);

            for (int i = 0; i < this.fpSpread1.ActiveSheet.ColumnCount; i++)
            {
                this.fpSpread1.ActiveSheet.Columns[i].Width = 100;
            }
            this.fpSpread1.ActiveSheet.Columns[5].Width = 150;
            this.fpSpread1.ActiveSheet.Columns[6].Width = 150;

            this.fpSpread1.ActiveSheet.RowCount = 38;

            this.fpSpread1.ActiveSheet.Cells[0, 5, 32, 6].BackColor = Color.LightSeaGreen;

            this.fpSpread1.ActiveSheet.AllowNoteEdit = true;
            this.fpSpread1.ActiveSheet.Cells[0, 5].NoteStyle = FarPoint.Win.Spread.NoteStyle.StickyNote;
            this.fpSpread1.ActiveSheet.Cells[0, 5].NoteIndicatorPosition = FarPoint.Win.Spread.NoteIndicatorPosition.TopLeft;
            this.fpSpread1.ActiveSheet.Cells[0, 5].NoteIndicatorSize = new Size(2,2);
            this.fpSpread1.ActiveSheet.Cells[0, 5].Note = "灰色背景区域用于计算债券相关数据，点击单元格可在公式栏查看公式信息";
        }

        private void SetFormla()
        {
            this.fpSpread1.ActiveSheet.Columns[5].Formula = "RECEIVED(A1,B1,C1,D1,E1)";
            this.fpSpread1.ActiveSheet.Columns[6].Formula = "TBILLPRICE(A1,B1,D1)";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FarPoint.Win.Spread.FormulaEditorUI formulaEditor = new FarPoint.Win.Spread.FormulaEditorUI(this.fpSpread1.Sheets[0]);
            formulaEditor.ShowDialog();
        }
    }
}
